package start;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;

public class JdbcActorDao implements ActorDao {
    private JdbcTemplate jdbcTemplate;
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    }

    public JdbcActorDao() {
        super();
    }

    public int[] batchUpdate(final List<Actor> actors) {
        int[] updateCounts =
            jdbcTemplate.batchUpdate("update t_actor set first_name=?, last_name=? where id=?", new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    ps.setString(1, actors.get(i).getFirstname());
                    ps.setString(2, actors.get(i).getLastname());
                    ps.setLong(3, actors.get(i).getId());
                }

                @Override
                public int getBatchSize() {
                    return actors.size();
                }
            });
        return updateCounts;
    }

    public int[] batchUpdate2(final List<Actor> actors) {
        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(actors.toArray());
        int[] updateCounts =
            namedParameterJdbcTemplate.batchUpdate("update t_actor set first_name = :firstname, last_name = :lastname where id = :id",
                                                   batch);
        return updateCounts;
    }

    public int[][] batchUpdate(final Collection<Actor> actors) {
        Collection<Object[]> batch = new ArrayList<Object[]>();
        for (Actor actor : actors) {
            Object[] values = new Object[] { actor.getFirstname(), actor.getLastname(), actor.getId() };
            batch.add(values);
        }
        int[][] updateCounts =
            jdbcTemplate.batchUpdate("update t_actor set first_name = ?, last_name = ? where id = ?", actors, 100,
                                     new ParameterizedPreparedStatementSetter<Actor>() {
                public void setValues(PreparedStatement ps, Actor argument) throws SQLException {
                    ps.setString(1, argument.getFirstname());
                    ps.setString(2, argument.getLastname());
                    ps.setLong(3, argument.getId());

                }
            });
        return updateCounts;
    }

    public void insert(Actor actor) {
        jdbcTemplate.update("insert into t_actor(first_name,last_name) values(?,?)",
                            new Object[] { actor.getFirstname(), actor.getLastname() });
    }


}
